
package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import model.core.utente.Commissario;
import model.core.utente.Studente;
import model.persistence.DAOFactory;

/**
 * 
 * @author Giuseppe Fusco (fusgius@gmail.com)
 *
 */
public class LoginPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 280884586651260043L;
	
	private JFrame parent;
	
	public LoginPanel(JFrame parent) {
        this.parent = parent;
		initComponents();
        addListener();
    }
    
    private void addListener(){
    	login.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				
				String mat = matricolaTextField.getText();
				String pas = String.valueOf(passwordTextField.getPassword());
				try {
					Studente s = (Studente) DAOFactory.createDAOManager_MySQL().getStudenteDao().retrieve(mat, pas);
					if(s!=null && (s instanceof Commissario)){
						System.out.println(s.getClass().getSimpleName());
						MainPanel m = new MainPanel(Commissario.createFromStudente(s));
						parent.setContentPane(m);
						parent.repaint();
						parent.revalidate();
					}else
						JOptionPane.showMessageDialog(getParent(), "Inserisci le credenziali corrette per accedere.");
				} catch (ClassNotFoundException | IOException | SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
    }

    private void initComponents() {

        register = new javax.swing.JButton();
        login = new javax.swing.JButton();
        passwordTextField = new javax.swing.JPasswordField();
        matricolaTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        register.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
        register.setText("Registrati");

        login.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
        login.setText("Accedi");

        passwordTextField.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N

        matricolaTextField.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
        jLabel2.setText("Password");

        jLabel1.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
        jLabel1.setText("Matricola");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passwordTextField)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(register)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(login)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(matricolaTextField))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(matricolaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(login)
                    .addComponent(register))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }
    
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton login;
    private javax.swing.JTextField matricolaTextField;
    private javax.swing.JPasswordField passwordTextField;
    private javax.swing.JButton register;
}
